using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.EII.Repository;
using System.Xml.Schema;
using System.Xml;
using OpenComposite.Mapper;
using System.Xml.Serialization;
using System.IO;
using OpenComposite.EII.Forms;
using OpenComposite.Base.Collections;

namespace OpenComposite.EII.Designer.Workflow
{
	public partial class FormLogicalActivityConfiguration : Form
	{
		#region Initialization
		public FormLogicalActivityConfiguration(Repository.Workflow workflow)
		{
			_workflow = workflow;

			InitializeComponent();

			init();
		}

		private void init()
		{
			showOperation();
		}

		#endregion

		#region Public Members

		#region Properties

		public Repository.Operation Operation
		{
			get { return _operation; }
		}
		public string InputConnections
		{
			get { return _inputConnections; }
		}
		public XsltDocument InputXSLT
		{
			get { return _inputXSLT; }
		}
		public string PhysicalInputConnections
		{
			get { return _inputConnectionsPhys; }
		}
		public XsltDocument PhysicalInputXSLT
		{
			get { return _inputXSLTPhys; }
		}
		//public string SelectedBObjectsString
		//{
		//    get
		//    {
		//        string s = "";
		//        foreach ( BusinessObject bo in lstDocumentSubscriptions.Items ) {
		//            if ( s != "" )
		//                s += cSeperator;
		//            s += bo.ID;
		//        }
		//        return s;
		//    }
		//}
		//public BusinessObject[] SelectedBObjects
		//{
		//    get
		//    {
		//        BusinessObject[] bobjects = new BusinessObject[lstDocumentSubscriptions.Items.Count];
		//        lstDocumentSubscriptions.Items.CopyTo(bobjects, 0);
		//        return bobjects;
		//    }
		//}

		#endregion

		#region Methods

		public void Init(string logactName, int operationId,
						 string inputConnections, string inputConnectionsPhysical,
						 IList<BusinessObject> selectedBObjects)
		{
			RepositoryService repSvc = (RepositoryService)_workflow.GetService(typeof(RepositoryService));
			if ( repSvc == null )
				throw new NullReferenceException("RepositoryService not available.");

			txtLogActName.Text = logactName;

			if ( operationId > 0 ) {
				_operation = repSvc.GetItem(ItemType.Operation, operationId, true) as Repository.Operation;
				showOperation();
			} else {
				_operation = null;
			}
			//// set input connections
			//_inputConnections = inputConnections;
			//_inputConnectionsPhys = inputConnectionsPhysical;
			//// set subscribed business objects
			//lstDocumentSubscriptions.Items.Clear();
			//foreach ( BusinessObject bo in selectedBObjects ) {
			//    lstDocumentSubscriptions.Items.Add(bo);
			//}
		}

		#endregion

		#endregion

		#region Private Members

		#region Constants
		private const char cSeperator = ';';
		#endregion

		#region Variables
		private Repository.Workflow _workflow = null;
		private Repository.Operation _operation = null;

		private XsltDocument _inputXSLT = null;
		private string _inputConnections = null;

		private XsltDocument _inputXSLTPhys = null;
		private string _inputConnectionsPhys = null;

		#endregion

		#region Methods

		private void selectOperation()
		{
			BindingListEx<Operation> lstOps = Global.Data.GetWorkflowOperation(_workflow.ID);
			if ( lstOps != null && lstOps.Count > 0 ) {
				Service svc = lstOps[0].GetService();
				if ( svc != null ) {
					Forms.FormSelectReferencedOperation frm =
						new OpenComposite.EII.Designer.Workflow.Forms.FormSelectReferencedOperation(svc);
					if ( frm.ShowDialog() == DialogResult.OK ) {
						_operation = frm.SelectedOperation;
						showOperation();
					}
				}
			}
		}
		
		private void showOperation()
		{
			if ( _operation != null ) {
				txtOperation.Text = _operation.Name;
				BusinessObject bo = _operation.GetInputBusinessObject();
				txtInputSchema.Text = ( bo != null ? bo.Name : "" );
				bo = _operation.GetOutputBusinessObject();
				txtOutputSchema.Text = ( bo != null ? bo.Name : "" );
			} else {
				txtOperation.Text = "Please select an operation.";
				txtInputSchema.Text = "";
				txtOutputSchema.Text = "";
			}
		}
		private void showSchema(string sSchema)
		{
			if ( string.IsNullOrEmpty(sSchema) )
				return;

			XmlDocument xd = new XmlDocument();
			xd.LoadXml(sSchema);

			formXmlViewer frm = new formXmlViewer();
			frm.XmlDocument = xd;
			frm.ShowDialog();
		}

		#endregion

		#region Event Handler

		private void btnSelectOperation_Click(object sender, EventArgs e)
		{
			selectOperation();
		}

		private void btnViewInputSchema_Click(object sender, EventArgs e)
		{
			if ( _operation != null )
				showSchema(_operation.InputSchemaString);
		}

		private void btnViewOutputSchema_Click(object sender, EventArgs e)
		{
			if ( _operation != null )
				showSchema(_operation.OutputSchemaString);
		}

		//private void btnAddDocument_Click(object sender, EventArgs e)
		//{
		//    selectBObject();
		//}

		//private void btnRemoveDocument_Click(object sender, EventArgs e)
		//{
		//    removeBObjects();
		//}

		//private void btnDefineInputMapping_Click(object sender, EventArgs e)
		//{
		//    setInputMapping();
		//}
		//private void btnDefinePhysMapping_Click(object sender, EventArgs e)
		//{
		//    setInputMappingPhysical();
		//}

		//private void comboOperation_SelectedIndexChanged(object sender, EventArgs e)
		//{
		//    txtInputSchema.Text = "";
		//    txtOutputSchema.Text = "";

		//    _operation = comboOperation.SelectedItem as Repository.Operation;
		//    if ( _operation != null ) {
		//        txtInputSchema.Text = _operation.InputSchemaString;
		//        txtOutputSchema.Text = _operation.OutputSchemaString;
		//    }
		//}

		private void tsbtnOK_Click(object sender, EventArgs e)
		{
			if ( /*_service == null || */_operation == null ) {
				MessageBox.Show("Please select an operation.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
				btnSelectOperation.Focus();
				return;
			}
			this.DialogResult = DialogResult.OK;
			this.Close();
		}
		private void tsbtnCancel_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
			this.Close();
		}

		#endregion

		#endregion

	}
}